/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.Entity;

import java.util.Hashtable;

/**
 *
 * @author Windows7
 */
public class ShoppingCart {

    private final static int INIT = 1;
    private Hashtable<String, ShoppingCartItem> cart;

    public ShoppingCart() {
        cart = new Hashtable<String, ShoppingCartItem>();

    }

    public Hashtable<String, ShoppingCartItem> getCart() {
        return cart;
    }

    public void setCart(Hashtable<String, ShoppingCartItem> cart) {
        this.cart = cart;
    }

    public void add2cart(ShoppingCartItem item) {

        if (cart.containsKey(item.getProductID())) {
            ShoppingCartItem cartItem = cart.get(item.getProductID());
            cartItem.setAmount(cartItem.getAmount() + 1);
            cartItem.setValue((float) (item.getUnitPrice() * cartItem.getAmount()));
        } else {
            item.setAmount(INIT);
            item.setValue((float) item.getUnitPrice());

            cart.put(item.getProductID(), item);
        }
    }

    public ShoppingCartItem[] getShoppingCartItems() {
        return cart.values().toArray(new ShoppingCartItem[cart.size()]);
    }

    //total cart
    public float getSubTotal() {
        float tt = 0.0f;
        for (ShoppingCartItem item : cart.values()) {
            tt += item.getValue();
        }
        return tt;
    }

    // check out
    public void deleteCart() {
        cart = new Hashtable<String, ShoppingCartItem>();
    }

    public void removeItemInCart(String proID) {
        cart.remove(proID);
        
    }

    public void edit(){
        
    }
}
